/*
 * Alvin Baldemeca
 * 
 * TCSS 305 - Spring 2012
 * Tetris Project Part 1
 * 
 * This package contains the pieces needed for the tetris game being developed.
 * The classes are mutable, however there are some defensive copying employed in
 * the code.  The model package all so contains test for the tetris pieces. 
 */
package model;
import java.util.Arrays;


/**
 * This class represents a J tetris piece.
 * The block can move left, right, down and does not rotate.
 * @author Alvin Baldemeca
 * @version 4/19/2012
 */
public class JPiece extends AbstractPiece
{
  /**
   * The rotation set for the J-Piece, the origin (0,0) representing 
   * the lower left hand corner.
   */  
  public static final int [][][] ROTATION = new int[][][] 
  { 
    {{0, 0}, {1, 0}, {1, 1}, {1, 2}},
    {{2, 0}, {0, 1}, {1, 1}, {2, 1}},
    {{1, 0}, {1, 1}, {1, 2}, {2, 2}},
    {{0, 1}, {1, 1}, {2, 1}, {0, 2}}
  };
  
  /**
   * Creates an J-piece with the given (x,y) coordinate. 
   * @param the_x the initial x coordinate
   * @param the_y the initial y coordinate
   */
  public JPiece(final int the_x, final int the_y)
  {
    super(the_x, the_y, ROTATION[NO_ROTATION].clone(), null);
  }
  
  /**
   * Creates an J-piece at (0,0) coordinate.
   */
  public JPiece() 
  {
    super(0, 0, ROTATION[NO_ROTATION].clone(), null);
  }

  /**
   * Creates a copy of an JPiece.
   * @param the_original the piece to copy
   */
  public JPiece(final JPiece the_original) 
  {
    super(the_original.getX(), the_original.getY(), the_original.getRotation().clone(), null);
  }
  /**
   * Creates an J-piece with the given (x,y) coordinate and rotation. 
   * @param the_x the initial x coordinate
   * @param the_y the initial y coordinate
   * @param the_rotation the initial rotation
   */
  public JPiece(final int the_x, final int the_y, final int the_rotation)
  {
    super(the_x, the_y, ROTATION[the_rotation].clone(), null);
  }
  /**
   * {@inheritDoc}
   */
  @Override
  public void rotate()
  {
    if (Arrays.deepEquals(ROTATION[NO_ROTATION], my_rotation))
    {
      System.arraycopy(ROTATION[FIRST_ROTATION], 0, my_rotation,
                     0, ROTATION[FIRST_ROTATION].length);
    }
    else if (Arrays.deepEquals(ROTATION[FIRST_ROTATION], my_rotation))
    {
      System.arraycopy(ROTATION[SECOND_ROTATION], 0, my_rotation,
                   0, ROTATION[SECOND_ROTATION].length); 
    }
    else if (Arrays.deepEquals(ROTATION[SECOND_ROTATION], my_rotation))
    {
      System.arraycopy(ROTATION[THIRD_ROTATION], 0, my_rotation,
                     0, ROTATION[THIRD_ROTATION].length);
    }
    else
    {
      System.arraycopy(ROTATION[NO_ROTATION], 0, my_rotation,
                   0, ROTATION[NO_ROTATION].length);
    }
    
    
  }
    

  /**
   * Creates a string representation of the tetris piece with its current
   * rotation using [] to represent a block.  
   * @return the shape of the tetris piece and its rotation.
   */
  @Override
  public String toString() 
  { 
    final StringBuilder sb = new StringBuilder();
    if (Arrays.deepEquals(ROTATION[NO_ROTATION], my_rotation))
    {
      sb.append("  []\n  []\n[][]");
    }
    else if (Arrays.deepEquals(ROTATION[FIRST_ROTATION], my_rotation))
    { 
      sb.append("[][][]\n    []");
      
    }
    else if (Arrays.deepEquals(ROTATION[SECOND_ROTATION], my_rotation))
    {
      sb.append("  [][]\n  []\n  []");
    }
    else
    { 
      sb.append("[]\n[][][]"); 
    }
    return sb.toString();
    
    
  }


}


